;(function($){
	$.fn.carousel=function(option){
		var defs = {
			imgs:[],
			delay:1000
		}
		var mytime;//延迟器对象
		var pos=0;//当前图片在第pos张上
		var userObj= $(this[0]);
		if(option){
			$.extend(defs,option);
		}
		
		//构建页面元素
		var initElement = function(){
			var str='<div class="carousel"><a href=""><img src="'
					+defs.imgs[pos]+'" alt="" /></a><div class="flag">';
			for (var ind=0;ind<defs.imgs.length;ind++) {
				str+='<span lang="'+ind+'"></span>';
			}
			str+='</div></div>';
			//讲组合好的元素放置到页面上
			userObj.append(str);
			//修改选择器的位置
			var width = userObj.width();
			var height = userObj.height();
			//计算flag图表的位置
			var imgsize = defs.imgs.length;
			var left = width/2-(imgsize*14+(imgsize-1)*10)/2;
			var top = height-40;
			$(".carousel .flag").css({"left":left+"px","top":top+"px"});
			//图片自动切换
			player();
			//用户选择图片处理
			$(".carousel .flag span").bind("mouseover",function(){
				clearTimeout(mytime);
				pos = $(this).attr("lang");
				opt();
				$(".carousel img").attr("src",defs.imgs[pos]);
			}).bind("mouseout",function(){
				player();
			})
		}
		
		//图片切换
		var player = function(){
			$(".carousel img").attr("src",defs.imgs[pos]);
			opt();
			mytime = setTimeout(function(){
				pos = ++pos>3?0:pos;
				player();
			},defs.delay);
		}
		
		//选择span激活效果
		var opt = function(){
			var spans = $(".carousel .flag span");
			spans.removeClass();
			$(spans[pos]).addClass("active");
		}
		
		
		
		initElement();
	}
})(jQuery);
